package com.qm.web;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import com.qm.model.ImportExcelReprotModel;
import com.qm.model.Classes;
import com.qm.model.Course;
import com.qm.model.Topic;
import com.qm.model.TopicTest;
import com.qm.model.Users;
import com.qm.service.AdminCourseService;
@Controller
@RequestMapping("/admin/courser")
public class AdminCourseController {

	@Autowired
	private AdminCourseService acourse;
	/**
	 * 课程信息分页
	 * @param pge
	 * @return
	 */
	@RequestMapping("CourseInformationPaging")
	public ModelAndView CourseInformationPaging(int page){
		ModelAndView mav=new ModelAndView();
		int count=5;//每页显示5行
		int pageStart=(page-1)*5;//数据库查询的起点
		int studnetCount=acourse.courseCount();//班级信息总数
		int totoalPage= (int) Math.ceil(studnetCount/(double)count);
		ArrayList<Course> course=acourse.selectCoursePaging(pageStart, count) ;//课程信息分页信息
		mav.addObject("page", page);//当前页面
		mav.addObject("courseInfo", course);//学生信息
		mav.addObject("totoalPage", totoalPage);//总共页码
		mav.addObject("menu", "CourseInformation");//当前menu
		mav.setViewName("/admin/Users/CourseInformation");
		return mav;
	}
	/**
	 * 根据课程代码查询课程信息
	 * @param cocourseCode
	 * @return
	 */
	@ResponseBody
	@RequestMapping("selectCourseBycocourseCode")
	public Course selectCourseBycocourseCode(int cocourseCode){
		Course course=acourse.selectCourseBycocourseCode(cocourseCode);
		return course;
		
	}
	/**
	 * 根据课程代码来修改课程信息
	 * @param cocourseCode
	 * @param coname
	 * @return
	 */
	@ResponseBody
	@RequestMapping("updateCourseBYcocourseCode")
	public int updateCourseBYcocourseCode(int cocourseCode,String coname){
		Course course=new Course();
		course.setConame(coname);
		course.setCocourseCode(cocourseCode);
		int result=acourse.updateCourseBYcocourseCode(course);
		return result;
		
	}
	/**
	 * 新增课程信息
	 * @param cocourseCode
	 * @param coname
	 * @return
	 */
	@ResponseBody
	@RequestMapping("insertCourse")
	public int insertCourse(int cocourseCode,String coname)
	{
		Course course=new Course();
		course.setCocourseCode(cocourseCode);
		course.setConame(coname);
		int result =acourse.insertCourse(course);
		return result;
	}
	/**
	 * 删除课程信息
	 * @param cocourseCode
	 * @return
	 */
	@ResponseBody
	@RequestMapping("deleteCourseByCocourseCode")
	public int deleteCourseByCocourseCode(int cocourseCode){
		int result=acourse.deleteCourseByCocourseCode(cocourseCode);
		return result;
	}
	/**
	 * 通过班级ID删除该班级
	 * @param cid
	 * @return
	 */
	@ResponseBody
	@RequestMapping("deleteClassByCid")
	public int deleteClassByCid(int cid){
		int result=acourse.deleteClassByCid(cid);
		return result;
	}
	/**
	 *班级信息分页
	 * @param page
	 * @return
	 */
	@RequestMapping("classesPading")
	public ModelAndView classesInformationPaging(int page){
		ModelAndView mav=new ModelAndView();
		int count=5;
		int pageStart=(page-1)*5;
		int studnetCount=acourse.classesInformationCount();//班级信息总数
		int totoalPage= (int) Math.ceil(studnetCount/(double)count);
		ArrayList<Classes> classInfo=acourse.classesInformationPaging(pageStart, count);//分页信息
		ArrayList<Course> courseName=acourse.selectCourseName();//选择数据库中的课程信息
		mav.addObject("courseName", courseName);//课程信息
		mav.addObject("page", page);//当前页面
		mav.addObject("classinfo", classInfo);//学生信息
		mav.addObject("totoalPage", totoalPage);//总共页码
		mav.addObject("menu", "ClassesInformation");//当前menu
		mav.setViewName("/admin/Users/ClassesInformation");
		return mav;
	}
	/**
	 * 新增班级
	 * @param cname
	 * @param ccouseCode
	 * @param cteacher
	 * @return
	 */
	@ResponseBody
	@RequestMapping("insertClass")
	public int insertClass(String cname,int ccouseCode,String cteacher){
		int ajaxResult=0;
		Classes insert=new Classes();
		
		insert.setCname(cname);
		insert.setCteacher(cteacher);
		insert.setCcouseCode(ccouseCode);
		int serviceReult=acourse.insertClass(insert);
		if(serviceReult==1){
			ajaxResult=1;
		}
		return ajaxResult; 
		
	}
	/**
	 * 通过班级编号来查询班级信息
	 * @param cid
	 * @return
	 */
	@ResponseBody
	@RequestMapping("selectClassByCid")
	public Classes selectClassByCid(int cid){
		Classes classes=acourse.selectClassByCid(cid);
		return classes;
	}
	/**
	 * 通过课程代码来选择当前代码的班级
	 * @param cocourseCode
	 * @param page
	 * @return
	 */
	@RequestMapping("selectClassesByCourse")
    public ModelAndView selectClassesByCourse(int cocourseCode,int page){
		ModelAndView mav=new ModelAndView();
		int count=5;//每页显示5行
		int pageStart=(page-1)*5;//数据库查询的起始位置
		int studnetCount=acourse.classesCountByCourse(cocourseCode);//总共的条数
		int totoalPage= (int) Math.ceil(studnetCount/(double)count);//向下取整
		ArrayList<Classes> classInfo=acourse.selectClassesByCourse(cocourseCode, pageStart, count);//传回对应的学生信息
		ArrayList<Course> courseName=acourse.selectCourseName();//选择数据库中的课程信息
		mav.addObject("courseName", courseName);
		mav.addObject("cocourseCode", cocourseCode);//当前的课程代码
		System.out.println("========================"+cocourseCode);
		mav.addObject("classinfo",classInfo);
		mav.addObject("page",page);
		mav.addObject("totoalPage",totoalPage);
		mav.addObject("menu", "ClassesInformation");//当前的menu名称
		mav.setViewName("/admin/Users/ClassesInformation");
		return mav;
	}
		
	/**
	 * 修改班级信息
	 * @param cid
	 * @param cname
	 * @param cteacher
	 * @param ccouseCode
	 * @return
	 */
	@ResponseBody
	@RequestMapping("updateClassByCid")
	public int updateClassByCid(int cid,String cname,String cteacher,int ccouseCode){
		Classes updateClass=new Classes();
		updateClass.setCid(cid);
		updateClass.setCname(cname);
		updateClass.setCteacher(cteacher);
		updateClass.setCcouseCode(ccouseCode);
		int result=acourse.updateClassByCid(updateClass);
		return result;
		
	}
    /**
     * 题目信息的显示
     * @return
     */
	@RequestMapping("SetsOfQuestions")
	public ModelAndView SetsOfQuestions(){
		ModelAndView mav =new ModelAndView();
		ArrayList<Course> courseName=acourse.selectCourseName();//选择数据库中的课程信息
		mav.addObject("courseName", courseName);
		mav.addObject("menu", "SetsOfQuestions");//当前的menu名称
		mav.setViewName("/admin/Users/SetsOfQuestions");
		return mav;
	}
    
	/**
	 * 导入题目信息
	 * @param excel
	 */
	@ResponseBody
	@RequestMapping("ImportTopics")
	public void insertStudents(MultipartFile excel,int tcourseCode){
	
		List<ImportExcelReprotModel<Topic>> topic=acourse.importTopics(excel, tcourseCode);

	}
	/**
	 * 查询某课程的题目信息
	 * @param cocourseCode
	 * @param page
	 * @return
	 */
	@RequestMapping("selectTopicByCourse")
	public ModelAndView selectTopicByCourse(int cocourseCode,int page){
		ModelAndView mav=new ModelAndView();
		int count=10;//每页显示5行
		int pageStart=(page-1)*10;//数据库查询的起点
		int studnetCount=acourse.selectTopicCountByCourse(cocourseCode);//信息总数
		int totoalPage= (int) Math.ceil(studnetCount/(double)count);
		ArrayList<TopicTest> course=acourse.selectTopicByCourse(cocourseCode, pageStart, count) ;//分页信息
		ArrayList<Course> courseName=acourse.selectCourseName();//选择数据库中的课程信息
		mav.addObject("courseName", courseName);//全部课程
		mav.addObject("cocourseCode", cocourseCode);//当前课程
		mav.addObject("page", page);//当前页面
		mav.addObject("topicByCourse", course);//学生信息
		mav.addObject("totoalPage", totoalPage);//总共页码
		mav.addObject("menu", "ExamsInformation");//当前menu
		mav.setViewName("/admin/Users/ExamsInformation");
		return mav;
		
	}
	/**
	 * 套题信息管理页面
	 * @return
	 */
	@RequestMapping("ExamsInformation")
	public ModelAndView  ExamsInformation(){
		ModelAndView mav=new ModelAndView();
		ArrayList<Course> courseName=acourse.selectCourseName();//选择数据库中的课程信息
		mav.addObject("courseName", courseName);
		mav.addObject("menu", "ExamsInformation");//当前menu
		mav.setViewName("/admin/Users/ExamsInformation");
		return mav;
		
	}

}
